package it.rap.applications.persistence.entities.filters;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Version;


@Entity
public class RoleTree extends TreeObject {

	@Id
	@GeneratedValue(strategy = GenerationType.TABLE)
	private BigDecimal id;	
	@Column(nullable = false)
	private String regexpPattern;
	private String description;
	@Column(nullable = false)
	private Boolean checked = false;
	@Version
	private Long version;

	@ManyToOne(optional = false)
	private FilterTree filterTree;
	
	@ManyToOne(optional = false)
	private RoleTree subRoleTree;

	@OneToMany( cascade = CascadeType.ALL, orphanRemoval=true, mappedBy="subRoleTree")
	private List<RoleTree> subRoleTreeList = new ArrayList<RoleTree>();

	public RoleTree(String name, String regexpPattern, String description) {
		super(name);
		this.regexpPattern = regexpPattern;
		this.description = description;
	}

	public RoleTree(RoleTree r) {
		super(r.getName());
		this.id = r.getId();
		this.regexpPattern = r.getRegexpPattern();
		this.description = r.getDescription();
		this.checked = r.isChecked();
	}
	
	public RoleTree() {
		super();
	}
	
	public BigDecimal getId() {
		return id;
	}

	public void setId(BigDecimal id) {
		this.id = id;
	}

	public String getRegexpPattern() {
		return regexpPattern;
	}

	public void setRegexpPattern(String regexpPattern) {
		this.regexpPattern = regexpPattern;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Boolean isChecked() {
		return checked;
	}

	public void setChecked(Boolean checked) {
		this.checked = checked;
	}

	public Long getVersion() {
		return version;
	}

	public void setVersion(Long version) {
		this.version = version;
	}
	
	public FilterTree getFilterTree() {
		return filterTree;
	}

	public void setFilterTree(FilterTree filterTree) {
		this.filterTree = filterTree;
	}
	
	public String toString() {
		return " - Role(" + getId() + ", " + getName() + ", "
				+ getRegexpPattern() + ", " + getDescription() + isChecked() + ")";
	}

	public RoleTree getSubRoleTree() {
		return subRoleTree;
	}

	public void setSubRoleTree(RoleTree subRoleTree) {
		this.subRoleTree = subRoleTree;
	}

	public List<RoleTree> getSubRoleTreeList() {
		return subRoleTreeList;
	}

	public void setSubRoleTreeList(List<RoleTree> subRoleTreeList) {
		this.subRoleTreeList = subRoleTreeList;
	}
}

